package com.wmm.job2;

import java.util.Scanner;

public class JobTwo {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("输入第一个字符串：");
        String s1 = sc.next();
        System.out.println("输入第二个字符串：");
        String s2 = sc.next();
        if (s1.length()>s2.length()){
            show(s2,s1);
        }else{
            show(s1,s2);
        }

    }

    /**
     * 寻找最长的公共子字符串
     * @param s1 长度短的字符串
     * @param s2 长度长的字符串
     */
    public static void show(String s1,String s2) {
        char[] ch1 = s1.toCharArray();
        char[] ch2 = s2.toCharArray();
        String result = "";
        //将短的字符串 逐个递减的跟长字符串进行匹配
        for (int i = 0; i < ch1.length; i++) {
            int n = i;
            String temp = "";
            for (int j = 0; j < ch2.length; j++)
                if (n < ch1.length && ch2[j] == ch1[n]) {
                    temp += ch1[n]; //存储匹配的字符
                    n++;
                } else {//从头开始
                    n = i;
                    if (temp.length() > result.length()) {
                        result = temp;
                        System.out.println("目前最大字符串是：" + result);
                    }
                    temp = "";
                }
        }
        System.out.println("最后最大的字符串：" + result);
    }

}
